const mysql= require('mysql');
module.exports={
    query: function(sql, params,callback) {
        // 1. 创建连接
        const connection = mysql.createConnection({
            host: 'localhost',
            user: 'root',
            password: '*********',
            database: 'myblog'
        });
        // 2. 连接数据库
        connection.connect((err)=>{
            if (err){
                console.log('数据库连接失败');
                throw err;
            }
        });
        // 3. 执行SQL语句
        connection.query(sql, params,  function(error, results, fields) {
            if (error){
                console.log('SQL语句执行失败');
                throw error;
            }
            callback(results);
        });
        // 4. 关闭连接
        connection.end((err)=>{
            if (err) {
                console.log('数据库关闭失败');
                throw err
            }
        });
    }
}